<?php

namespace Welcomescreen\Objects;
use Welcomescreen\Exceptions;
use Welcomescreen\Agavi\Model;

class Visit {
	/**
	 * @var int
	 */
	protected $id;
	
	/**
	 * @var int
	 */
	protected $revision;
	
	/**
	 * @var bool
	 */
	protected $manualActivationOverride;
	
	/**
	 * @var DateTime
	 */
	protected $startDate;
	
	/**
	 * @var DateTime
	 */
	protected $endDate;
	
	/**
	 * @var string
	 */
	protected $visitorName;
	
	/**
	 * @var Welcomescreen\Objects\User
	 */
	protected $lastEditUser;
	
	/**
	 * @var string
	 */
	protected $templateName;
	
	/**
	 * @var array(string => mixed)
	 */
	protected $templateData;
	
	/**
	 * @param array $data
	 * @param UserManagerModel $userManager
	 */
	public static function initFromArray(array $data, \UserManagerModel $userManager) {
		foreach(get_class_vars(__CLASS__) as $propertyName => $value) {
			if(!array_key_exists($propertyName, $data)) {
				throw new Exceptions\MissingPropertyException('Missing required property "' . $propertyName . '" in fromArray()');
			}
		}
		$obj = new self();
		$obj->setId($data['id']);
		$obj->setRevision($data['revision']);
		$obj->setManualActivationOverride($data['manualActivationOverride']);
		$obj->setStartDate($data['startDate'] ? new \DateTime($data['startDate']) : null);
		$obj->setEndDate($data['endDate'] ? new \DateTime($data['endDate']) : null);
		$obj->setVisitorName($data['visitorName']);
		$obj->setLastEditUser($userManager->getUser($data['lastEditUser']));
		$obj->setTemplateName($data['templateName']);
		$obj->setTemplateData($data['templateData']);
		
		return $obj;
	}
	
	/**
	 * @return array(string => mixed)
	 */
	public function toArray() {
		return array(
			'id' => $this->getId(),
			'revision' => $this->getRevision(),
			'active' => $this->isActive(),
			'manualActivationOverride' => $this->getManualActivationOverride(),
			'startDate' => $this->getStartDate() ? $this->getStartDate()->format(\DateTime::ISO8601) : null,
			'endDate' => $this->getEndDate() ? $this->getEndDate()->format(\DateTime::ISO8601) : null,
			'visitorName' => $this->getVisitorName(),
			'lastEditUser' => $this->getLastEditUser()->getName(),
			'templateName' => $this->getTemplateName(),
			'templateData' => $this->getTemplateData(),
		);
	}
	
	/**
	 * Retrieves whether this visit is currently active and should be displayed
	 *
	 * @return bool
	 */
	public function isActive() {
		// if the startDate is null, this visit should always be displayed
		if($this->manualActivationOverride || $this->isDefault()) {
			return true;
		}
		
		$now = new \DateTime();
		return $this->startDate <= $now && $now < $this->endDate;
	}
	
	/**
	 * Retrieves whether this visit should be used as 'default' visit (A visit which should always be shown if no "normal" visits exist)
	 * 
	 * @return bool
	 */
	public function isDefault() {
		return $this->getStartDate() === null;
	}
	
	/**
	 * @return int
	 */
	public function getId() {
		return $this->id;
	}
	
	/**
	 * @param int $id
	 */
	public function setId($id) {
		$this->id = $id;
	}
	
	/**
	 * @return int
	 */
	public function getRevision() {
		return $this->revision;
	}
	
	/**
	 * @param int $revision
	 */
	public function setRevision($revision) {
		$this->revision = $revision;
	}
	
	/**
	 * @return bool
	 */
	public function getManualActivationOverride() {
		return $this->manualActivationOverride;
	}
	
	/**
	 * @param string $manualActivationOverride
	 */
	public function setManualActivationOverride($manualActivationOverride) {
		$this->manualActivationOverride = $manualActivationOverride;
	}
	
	/**
	 * @return \DateTime
	 */
	public function getStartDate() {
		return $this->startDate;
	}
	
	/**
	 * @param \DateTime $startDate
	 */
	public function setStartDate(\DateTime $startDate = null) {
		$this->startDate = $startDate;
	}
	
	/**
	 * @return \DateTime
	 */
	public function getEndDate() {
		return $this->endDate;
	}
	
	/**
	 * @param \DateTime $endDate
	 */
	public function setEndDate(\DateTime $endDate = null) {
		$this->endDate = $endDate;
	}
	
	/**
	 * @return string
	 */
	public function getVisitorName() {
		return $this->visitorName;
	}
	
	/**
	 * @param string $visitorName
	 */
	public function setVisitorName($visitorName) {
		$this->visitorName = $visitorName;
	}
	
	/**
	 * @return Welcomescreen\Objects\User
	 */
	public function getLastEditUser() {
		return $this->lastEditUser;
	}
	
	/**
	 * @param Welcomescreen\Objects\User $lastEditUser
	 */
	public function setLastEditUser(User $lastEditUser) {
		$this->lastEditUser = $lastEditUser;
	}
	
	/**
	 * @return string
	 */
	public function getTemplateName() {
		return $this->templateName;
	}
	
	/**
	 * @param string $templateName
	 */
	public function setTemplateName($templateName) {
		$this->templateName = $templateName;
	}
	
	/**
	 * @return array
	 */
	public function getTemplateData() {
		return $this->templateData;
	}
	
	/**
	 * @param array $templateData
	 */
	public function setTemplateData(array $templateData) {
		$this->templateData = $templateData;
	}
	
	/**
	 * @param string $name
	 * @param mixed $value
	 */
	public function setTemplateDataEntry($name, $value) {
		$this->templateData[$name] = $value;
	}
}

?>